﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using IView;
using Presenter;

namespace JSKE
{
    public partial class ScheduleReportForm : FormLang, IScheduleReport
    {
        private PresenterScheduleReport _presenter;
        private BindingSource _bindingSource = new BindingSource();
        DataTable dtbSumaryReport = new DataTable();
        DataTable dtbTaskReport = new DataTable();
        public ScheduleReportForm()
        {
            //InitializeComponent();
            _presenter = new PresenterScheduleReport(this, DATA_MODE);
            dtbSumaryReport = _presenter.loadSummaryReport();
            dtbTaskReport = _presenter.loadTaskReport();        
            if (dtbSumaryReport.Rows.Count == 0)
            {
                MessageBox.Show("There are no reports to be shown!", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                loadSummaryReport();
                this.ShowDialog();
                rdbSummaryRept.Checked = true;
            }
        }

        private void populateData2ListView(DataTable dtb)
        {
            listViewReport.SuspendLayout();
            listViewReport.Columns.Clear();
            
            listViewReport.Items.Clear();
            int numOfCol = dtb.Columns.Count;

            for (int i = 0; i < numOfCol; i++)
            {
                ColumnHeader clh = new ColumnHeader();
                clh.Text = dtb.Columns[i].ColumnName;
                listViewReport.Columns.Add(clh);
            }
            for(int i =0; i < dtb.Rows.Count; i++)
            {
                DataRow dtr = dtb.Rows[i];
                String[] rowContent = new String[numOfCol];
                for (int j = 0; j < numOfCol; j++)
                {
                    rowContent[j] = dtr[j].ToString();
                }                
                ListViewItem item = new ListViewItem(rowContent);
                if (!dtr["Late"].ToString().Trim().Equals(""))
                {
                    item.BackColor = Color.Yellow;
                }
                listViewReport.Items.Add(item);
            }
            listViewReport.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            listViewReport.ResumeLayout();

        }
        private void loadTaskReport()
        {
            
            populateData2ListView(dtbTaskReport);

        }
        private void loadSummaryReport()
        {
            populateData2ListView(dtbSumaryReport);
        }

        private void rdbSummaryRept_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSummaryRept.Checked)
            {
                loadSummaryReport();
            }
        }

        private void rdbTaskReport_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTaskReport.Checked)
            {
                loadTaskReport();
            }
        }

        private void listViewTaskReport_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (rdbTaskReport.Checked)
                {
                    int taskID = int.Parse(dtbTaskReport.Rows[listViewReport.SelectedIndices[0]][0].ToString());
                    TaskForm taskForm = new TaskForm(_presenter.getTaskByID(taskID));
                    taskForm.ShowDialog(this);
                }

            }
            catch
            {
                MessageBox.Show("You clicked on the wrong place",
                    "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
